﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;

public partial class Admin_UploadFile : System.Web.UI.Page
{
	private int nColumnID;		// 当前栏目ID
	private string strPath;		// 文件上传的目录
	private string strVirtualPath;		// 文件上传的虚拟路径
	private Column clsNowColumn;		// 当前栏目对象
	private ColumnComponent clsColumnComp = new ColumnComponent();
	private UploadFileComponent clsUploadFileComp = new UploadFileComponent();

	protected void Page_Load(object sender, EventArgs e)
	{
		if (Session["LoginUserID"] == null)
		{
			Response.Write("<script language=javascript>parent.location.href='Login.aspx';</script>");
		}

		nColumnID = Convert.ToInt32(Request.QueryString["id"]);

		clsNowColumn = clsColumnComp.GetOneByID(nColumnID);

		LabColumnName.Text = clsNowColumn.Name;		// 设置显示当前栏目名称

		strPath = Server.MapPath("..") + "\\Files\\";	// 文件上传的跟目录

		strPath = strPath + (CommonFunction.GetMd5Hash(CommonFunction.GetNowYearMonth())) + "\\";

		strVirtualPath = "\\Files\\" + (CommonFunction.GetMd5Hash(CommonFunction.GetNowYearMonth())) + "\\";

		//Response.Write(strPath);

		////////////////////////////////////////////////////////////////////////////////
		// 控件事件相应函数添加
		BtnUpload.Click += new EventHandler(BtnUpload_OnClick);				// 上传按钮
		BtnShowDate.Click += new EventHandler(BtnShowDate_Click);			// 显示或隐藏日期控件
		CalDate.SelectionChanged += new EventHandler(CalDate_SelectionChanged);		// 日期控件
		////////////////////////////////////////////////////////////////////////////////

		if (!IsPostBack)
		{
			BindCheckBoxList(CblistColumns);		// 显示用户有权限上传的栏目

			TxtDate.Text = DateTime.Now.ToString("yyyy-MM-dd");		// 上传日期显示为当前日期

		}
	}

	
	#region 控件绑定
	/// <summary>
	/// <para>功能描述：	绑定用户有权限上传的栏目</para>
	/// <para>输入参数：	</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-09</para>
	/// <para>备注：	</para>
	/// </summary>
	private void BindCheckBoxList(CheckBoxList cbList)
	{
		cbList.DataSource = clsColumnComp.QueryHaveRightColumns(Session["LoginUserRight"].ToString());
		cbList.DataTextField = "Name";
		cbList.DataValueField = "ID";
		cbList.DataBind();
	}
	#endregion

	#region 按钮按下相应函数

	/// <summary>
	/// <para>功能描述：	上传文件</para>
	/// <para>输入参数：	</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-09</para>
	/// <para>备注：	</para>
	/// </summary>
	protected void BtnUpload_OnClick(object sender, EventArgs e)
	{
		string strNewFileName;			// 程序自动产生的上传文件名字
		string strFileExtension;
		UploadFile clsUploadFile;
		UploadFile clsShow;
		Boolean bSelectColumn;			// 用户是否选择上传的栏目
		Boolean bOKUpload;			// 上传文件是否成功

		clsUploadFile = new UploadFile();
		clsShow = new UploadFile();
		bSelectColumn = false;
		bOKUpload = true;

		if (UploadFile.HasFile)
		{
			strNewFileName = CommonFunction.GetMd5Hash(CommonFunction.GetNowDateTime());
			strFileExtension = Path.GetExtension(UploadFile.FileName);
			FileOperation.CreateFolder(strPath);
			UploadFile.SaveAs(strPath + strNewFileName + strFileExtension);

			byte[] bitUpload = new byte[UploadFile.PostedFile.ContentLength - 1];
			bitUpload = UploadFile.FileBytes;

			if (CblistColumns.Items.Count > 0)
			{
				for (int i = 0; i < CblistColumns.Items.Count; i++)
				{
					if (CblistColumns.Items[i].Selected == true)
					{
						clsUploadFile.FileName = UploadFile.FileName;
						clsUploadFile.Path = strVirtualPath + strNewFileName + strFileExtension;
						clsUploadFile.ColumnID = Convert.ToInt32(CblistColumns.Items[i].Value);
						clsUploadFile.ShowDate = TxtDate.Text;
						clsUploadFile.DateTime = DateTime.Now.ToString();
						clsUploadFile.Count = 0;
						clsUploadFile.UserID = Convert.ToInt32(Session["LoginUserID"]);
						clsUploadFile.Content = bitUpload;
						clsUploadFile.Type = UploadFile.PostedFile.ContentType;

						if (!clsUploadFileComp.Add(clsUploadFile))
						{
							bOKUpload = false;
						}

						bSelectColumn = true;
					}
				}
				if (!bSelectColumn)
				{
					ShowMessageAlert.ShowMessage("请选择上传的栏目！");
				}
				if (!bOKUpload)
				{
					ShowMessageAlert.ShowMessage("上传失败");
				}
				else
				{
					ShowMessageAlert.ShowMessage("上传成功！");
				}
			}
				
		}
		else
		{
			ShowMessageAlert.ShowMessage("请选择上传文件！");
		}

		//clsShow = clsUploadFileComp.GetOne(7);

		////bArrShow = System.Text.Encoding.Default.GetBytes(clsShow.Content);

		//Response.Clear();
		//Response.AddHeader("Content-Type", "application/msword");
		//Response.BinaryWrite(clsShow.Content);
		//Response.Flush();
		//Response.End();
	}

	/// <summary>
	/// 显示或隐藏选择日期
	/// </summary>
	/**********************************************************************************************
	 ** 功能描述：	显示或隐藏选择日期
	 ** 输入参数：
	 ** 返回值：
	 ** 作者：	Tony Mucor
	 ** 日期：	2013-08-14
	 ** 修改：
	 ** 日期：
	 *********************************************************************************************/
	void BtnShowDate_Click(object sender, EventArgs e)
	{
		DynamicDateDiv.Visible = !DynamicDateDiv.Visible;
	}
	#endregion

	#region 日期控件事件相应函数
	protected void CalDate_SelectionChanged(object sender, EventArgs e)
	{
		TxtDate.Text = CalDate.SelectedDate.ToString("yyyy-MM-dd");
		DynamicDateDiv.Visible = !DynamicDateDiv.Visible;
	}
	#endregion
}
